﻿<Window x:Class="ChatBox.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:vm="clr-namespace:ChatBox.ViewModels"
        xmlns:m="clr-namespace:ChatBox.Models"
        xmlns:ui="clr-namespace:ChatBox.UI"
        xmlns:util="clr-namespace:ChatBox.Utilities"
        Title="ChatBox"
        Icon="/ChatBox.ico"
        Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
  <Window.Resources>
    <Style x:Key="SmallIconImage" TargetType="{x:Type Image}">
      <Setter Property="Width" Value="16"/>
      <Setter Property="Height" Value="16"/>
      <Setter Property="Stretch" Value="Uniform"/>
      <Setter Property="SnapsToDevicePixels" Value="True"/>
      <Setter Property="RenderOptions.BitmapScalingMode" Value="NearestNeighbor"/>
      <Setter Property="RenderOptions.EdgeMode" Value="Aliased"/>
      <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
          <Setter Property="Opacity" Value="0.5"/>
        </Trigger>
      </Style.Triggers>
    </Style>

    <Style x:Key="{x:Type ui:MultiSizeImage}" TargetType="{x:Type ui:MultiSizeImage}" BasedOn="{StaticResource SmallIconImage}"/>

    <DataTemplate x:Key="ChannelTreeTemplate" DataType="{x:Type m:ChannelInfo}">
      <StackPanel Orientation="Horizontal" Height="21">
        <StackPanel.Style>
          <Style TargetType="{x:Type StackPanel}">
            <Setter Property="Background" Value="Transparent"/>
            <Style.Triggers>
              <DataTrigger Binding="{Binding Path=Status}" Value="{x:Static m:ChannelStatus.NewMessage}">
                <DataTrigger.EnterActions>
                  <BeginStoryboard>
                    <Storyboard>
                      <ColorAnimation To="{x:Static SystemColors.HighlightColor}" Duration="0:0:1" AutoReverse="True" RepeatBehavior="Forever" FillBehavior="Stop" Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"/>
                    </Storyboard>
                  </BeginStoryboard>
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                  <BeginStoryboard>
                    <Storyboard>
                      <ColorAnimation To="Transparent" Duration="0:0:1" FillBehavior="Stop" Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"/>
                    </Storyboard>
                  </BeginStoryboard>
                </DataTrigger.ExitActions>
              </DataTrigger>
            </Style.Triggers>
          </Style>
        </StackPanel.Style>
        <StackPanel.ContextMenu>
          <ContextMenu>
            <MenuItem Header="Remove">
              <MenuItem.Icon>
                <ui:MultiSizeImage Source="/Resources/Icons/Remove.ico" Style="{StaticResource SmallIconImage}"/>
              </MenuItem.Icon>
            </MenuItem>
          </ContextMenu>
        </StackPanel.ContextMenu>
        <ui:MultiSizeImage Source="/Resources/Icons/Channel.ico" Style="{StaticResource SmallIconImage}" VerticalAlignment="Center"/>
        <TextBlock Name="textChannelName" Text="{Binding Path=Name}" Margin="4,0" VerticalAlignment="Center">
          <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">
              <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
              <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Status}" Value="{x:Static m:ChannelStatus.NewMessage}">
                  <DataTrigger.EnterActions>
                    <BeginStoryboard>
                      <Storyboard>
                        <ColorAnimation To="{x:Static SystemColors.HighlightTextColor}" Duration="0:0:1" AutoReverse="True" RepeatBehavior="Forever" FillBehavior="Stop" Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)"/>
                      </Storyboard>
                    </BeginStoryboard>
                  </DataTrigger.EnterActions>
                  <DataTrigger.ExitActions>
                    <BeginStoryboard>
                      <Storyboard>
                        <ColorAnimation To="{x:Static SystemColors.ControlTextColor}" Duration="0:0:1" FillBehavior="Stop" Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)"/>
                      </Storyboard>
                    </BeginStoryboard>
                  </DataTrigger.ExitActions>
                </DataTrigger>
              </Style.Triggers>
            </Style>
          </TextBlock.Style>
        </TextBlock>
      </StackPanel>
    </DataTemplate>

    <HierarchicalDataTemplate x:Key="ServerTreeTemplate" DataType="{x:Type m:ServerInfo}" ItemsSource="{Binding Path=Channels}" ItemTemplate="{StaticResource ChannelTreeTemplate}">
      <StackPanel Orientation="Horizontal" Height="21">
        <StackPanel.ContextMenu>
          <ContextMenu>
            <MenuItem Header="Connect to Server...">
              <MenuItem.Icon>
                <ui:MultiSizeImage Source="/Resources/Icons/ConnectServer.ico" Style="{StaticResource SmallIconImage}"/>
              </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Disconnect from Server">
              <MenuItem.Icon>
                <ui:MultiSizeImage Source="/Resources/Icons/DisconnectServer.ico" Style="{StaticResource SmallIconImage}"/>
              </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Add channel...">
              <MenuItem.Icon>
                <ui:MultiSizeImage Source="/Resources/Icons/Add.ico" Style="{StaticResource SmallIconImage}"/>
              </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Remove">
              <MenuItem.Icon>
                <ui:MultiSizeImage Source="/Resources/Icons/Remove.ico" Style="{StaticResource SmallIconImage}"/>
              </MenuItem.Icon>
            </MenuItem>
          </ContextMenu>
        </StackPanel.ContextMenu>
        <ui:MultiSizeImage VerticalAlignment="Center">
          <ui:MultiSizeImage.Style>
            <Style TargetType="{x:Type ui:MultiSizeImage}" BasedOn="{StaticResource SmallIconImage}">
              <Setter Property="Source" Value="/Resources/Icons/ServerConnected.ico"/>
              <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Status}" Value="{x:Static m:ServerStatus.NotConnected}">
                  <Setter Property="Source" Value="/Resources/Icons/ServerNotConnected.ico"/>
                </DataTrigger>
              </Style.Triggers>
            </Style>
          </ui:MultiSizeImage.Style>
        </ui:MultiSizeImage>
        <TextBlock Text="{Binding Path=Name}" Margin="4,0" VerticalAlignment="Center"/>
      </StackPanel>
    </HierarchicalDataTemplate>

    <DataTemplate x:Key="ServerTemplate" DataType="{x:Type m:ServerInfo}">
      <GroupBox Header="{Binding Path=Name}" Margin="0,4,4,4">
        <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
          </Grid.RowDefinitions>

          <!-- Chat Display -->
          <ItemsControl Grid.Row="0" Margin="4"/>

          <!-- Text Input Section -->
          <DockPanel Grid.Row="1">
            <Button Content="_Send" DockPanel.Dock="Right" Command="{Binding Path=Send}" CommandParameter="{Binding ElementName=Message, Path=Text}" Margin="4"/>

            <TextBox Text="{Binding Path=Message}" Name="Message" Margin="4"/>
          </DockPanel>
        </Grid>
      </GroupBox>
    </DataTemplate>

    <DataTemplate x:Key="ChannelTemplate" DataType="{x:Type m:ChannelInfo}">
      <GroupBox Header="{Binding Path=Name}" Margin="0,4,4,4">
        <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
          </Grid.RowDefinitions>

          <Grid>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="75*"/>
              <ColumnDefinition Width="25*"/>
            </Grid.ColumnDefinitions>

            <!-- Chat Display -->
            <ItemsControl Grid.Column="0" Margin="4"/>

            <!-- Channel Users List -->
            <ListBox ItemsSource="{Binding Path=Users}" DisplayMemberPath="Name" Grid.Column="1" Margin="4"/>
          </Grid>

          <!-- Text Input Section -->
          <DockPanel Grid.Row="1">
            <Button Content="_Send" DockPanel.Dock="Right" Command="{Binding Path=Send}" CommandParameter="{Binding ElementName=Message, Path=Text}" Margin="4"/>

            <TextBox Text="{Binding Path=Message}" Name="Message" Margin="4"/>
          </DockPanel>
        </Grid>
      </GroupBox>
    </DataTemplate>

    <DataTemplate x:Key="NullTemplate">
      <GroupBox Header="ChatBox" Margin="4">
        <StackPanel Orientation="Vertical">
          <TextBlock Text="Welcome to ChatBox!" FontWeight="Bold" Margin="4,0"/>
          <TextBlock Text="Please select a server or a channel." Margin="4,0"/>
        </StackPanel>
      </GroupBox>
    </DataTemplate>

    <util:MessageRepositoryTemplateSelector x:Key="MessageRepositoryTemplateSelector" ServerTemplate="{StaticResource ServerTemplate}" ChannelTemplate="{StaticResource ChannelTemplate}" NullTemplate="{StaticResource NullTemplate}"/>
  </Window.Resources>
  <Border Padding="4">
    <DockPanel>
      <Menu DockPanel.Dock="Top">
        <MenuItem Header="_File">
          <MenuItem Header="E_xit" Click="Exit_Click">
            <MenuItem.Icon>
              <ui:MultiSizeImage Source="/Resources/Icons/Exit.ico"/>
            </MenuItem.Icon>
          </MenuItem>
        </MenuItem>
        <MenuItem Header="_Edit">
          <MenuItem Header="_Undo" Command="Undo">
            <MenuItem.Icon>
              <ui:MultiSizeImage Source="/Resources/Icons/Undo.ico"/>
            </MenuItem.Icon>
          </MenuItem>
          <MenuItem Header="_Redo" Command="Redo">
            <MenuItem.Icon>
              <ui:MultiSizeImage Source="/Resources/Icons/Redo.ico"/>
            </MenuItem.Icon>
          </MenuItem>
          <Separator/>
          <MenuItem Header="Cu_t" Command="Cut">
            <MenuItem.Icon>
              <ui:MultiSizeImage Source="/Resources/Icons/Cut.ico"/>
            </MenuItem.Icon>
          </MenuItem>
          <MenuItem Header="_Copy" Command="Copy">
            <MenuItem.Icon>
              <ui:MultiSizeImage Source="/Resources/Icons/Copy.ico"/>
            </MenuItem.Icon>
          </MenuItem>
          <MenuItem Header="_Paste" Command="Paste">
            <MenuItem.Icon>
              <ui:MultiSizeImage Source="/Resources/Icons/Paste.ico"/>
            </MenuItem.Icon>
          </MenuItem>
          <Separator/>
          <MenuItem Header="_Settings..." Command="{Binding Path=Settings}">
            <MenuItem.Icon>
              <ui:MultiSizeImage Source="/Resources/Icons/Settings.ico"/>
            </MenuItem.Icon>
          </MenuItem>
        </MenuItem>
        <MenuItem Header="_Help">
          <MenuItem Header="_View Help" Command="Help">
            <MenuItem.Icon>
              <ui:MultiSizeImage Source="/Resources/Icons/Help.ico"/>
            </MenuItem.Icon>
          </MenuItem>
          <MenuItem Header="_Check for Updates...">
            <MenuItem.Icon>
              <ui:MultiSizeImage Source="/Resources/Icons/Update.ico"/>
            </MenuItem.Icon>
          </MenuItem>
          <Separator/>
          <MenuItem Header="_About..." Command="{Binding Path=About}">
            <MenuItem.Icon>
              <ui:MultiSizeImage Source="/ChatBox.ico"/>
            </MenuItem.Icon>
          </MenuItem>
        </MenuItem>
      </Menu>

      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="25*"/>
          <ColumnDefinition Width="Auto"/>
          <ColumnDefinition Width="75*"/>
        </Grid.ColumnDefinitions>

        <!-- Servers and channels list -->
        <GroupBox Header="Servers and channels" Grid.Row="0" Margin="4,4,0,4">
          <GroupBox.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
              <Setter Property="IsExpanded" Value="True"/>
            </Style>
          </GroupBox.Resources>
          <TreeView Name="TreeServers" Margin="4">
            <i:Interaction.Triggers>
              <i:EventTrigger EventName="SelectedItemChanged">
                <i:InvokeCommandAction Command="{Binding SetCurrentItem}" CommandParameter="{Binding ElementName=TreeServers, Path=SelectedItem}"/>
              </i:EventTrigger>
            </i:Interaction.Triggers>
            <TreeViewItem ItemsSource="{Binding Path=Servers}" ItemTemplate="{StaticResource ServerTreeTemplate}">
              <TreeViewItem.Header>
                <StackPanel Orientation="Horizontal" Height="21">
                  <StackPanel.ContextMenu>
                    <ContextMenu>
                      <MenuItem Header="Add server...">
                        <MenuItem.Icon>
                          <ui:MultiSizeImage Source="/Resources/Icons/Add.ico" Style="{StaticResource SmallIconImage}"/>
                        </MenuItem.Icon>
                      </MenuItem>
                    </ContextMenu>
                  </StackPanel.ContextMenu>
                  <ui:MultiSizeImage Source="/Resources/Icons/Servers.ico" Style="{StaticResource SmallIconImage}" VerticalAlignment="Center"/>
                  <TextBlock Text="Servers" Margin="4,0" VerticalAlignment="Center" Background="Transparent"/>
                </StackPanel>
              </TreeViewItem.Header>
            </TreeViewItem>
          </TreeView>
        </GroupBox>

        <GridSplitter Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Stretch" ShowsPreview="False" Width="8"/>

        <ContentPresenter Grid.Column="2" Content="{Binding ElementName=TreeServers, Path=SelectedItem}" ContentTemplateSelector="{StaticResource MessageRepositoryTemplateSelector}"/>
      </Grid>
    </DockPanel>
  </Border>
</Window>
